const app = getApp()
import {
    getWorker,
    setRemarks
} from '../../../api/app.js'
import user from '../../../utils/user.js'

Page({
    data: {
        workerId: '',
        worker: {},
        showCardStatus: false,
        isAdmin: false,
        isBank: false,
        isCard: false,
        nickName: '',
        isNickNamePopup: false,
        userId: ''
    },
    async onLoad(options) {
        await user.waitLogin()
        // 初始化获得员工基本信息
        this.setData({
            userId: options.id || ''
        })
        await this.onGetWorker()
    },
    onGetWorker() {
        getWorker({
            userId: this.data.userId || ''
        }).then(res => {
            console.log(res, 'csjres');
            this.setData({
                worker: res,
                workerId: this.data.userId,
                nickName: res.nickName || '',
                isAdmin: user.isAdmin,
                // 'worker.ownIdCardFront':'https://i.postimg.cc/qR9sw7qr/2134c2ee9db24ddb248db5f3ec46cbb.jpg',
                // 'worker.ownIdCardBack':'https://i.postimg.cc/qR9sw7qr/2134c2ee9db24ddb248db5f3ec46cbb.jpg'
            })
        })
    },
    // 拨打员工电话
    onCall() {
        wx.makePhoneCall({
            phoneNumber: this.data.worker.phone
        })
    },
    // 点击显示身份证
    onShowCard() {
        this.setData({
            showCardStatus: !this.data.showCardStatus
        })
    },
    // 点击跳转到员工工作列表界面
    onQueryDetail(event) {
        wx.navigateTo({
            url: '/pages/worker/record/record?userId=' + this.data.workerId + '&projectId=' + event.currentTarget.dataset.id
        })
    },
    // 网商银行
    onEyeCloseBank() {
        this.setData({
            isBank: !this.data.isBank
        })
    },
    // 银行卡号
    onEyeCloseCard() {
        this.setData({
            isCard: !this.data.isCard
        })
    },
    // 关闭弹窗
    onCancel() {
        this.setData({
            isNickNamePopup: false
        })
    },
    onNickName(event) {
        console.log(event.detail.value, 'event')
        this.setData({
            nickName: this.data.nickName.replace(/[^\u4E00-\u9FA5]/g, '')
        })
    },
    // 打开设置备注弹窗
    onNickNamePopup() {
        this.setData({
            isNickNamePopup: true
        })
    },
    // 设置备注
    async onSetRemarks() {
        // if(!this.data.nickName){
        //     this.setData({
        //         isNickNamePopup:false
        //     })
        //     return
        // }
        await setRemarks({
            userId: this.data.userId,
            companyId: user.companyId,
            nickName: this.data.nickName
        })
        this.onCancel()
        app.toast('修改成功')
        setTimeout(async () => {
            await this.onGetWorker()
        }, 500)
        // 
    },
    onPreviewImage(event) {
        console.log(event, 'event')
        const {
            url
        } = event.currentTarget.dataset
        wx.previewImage({
            urls: [url],
        })
    }
})